package com.dog.view.controller;

import com.dog.model.*;
import com.dog.service.IVacancyService;
import com.dog.spring.ServiceFactory;
import com.dog.view.beans.VacancyBean;
import com.dog.view.utils.EnumSelectItems;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * User: Kuttubek Aidaraliev
 * Date: 06.04.2010
 * Time: 13:35:27
 */
@Controller
@Scope(value=WebApplicationContext.SCOPE_REQUEST)
public class VacancyBeanController extends BaseBeanController<IVacancyService, Vacancy, VacancyBean> {


    @Autowired
    private IVacancyService vacancyService;
    //state is closed
    //private boolean closed = false;
    //private boolean editingContactPersons = false;

    public VacancyBeanController(){
    }

    @PostConstruct
    protected void initialized(){
        vacancyService = ServiceFactory.getVacancyService();
       setService(vacancyService); 
    }

    @Override
    public String list() {
        return "vacancyList";
    }

    @Override
    protected String form() {
        return "vacancy";
    }

    @Override
    protected Vacancy newEntity() {
        Vacancy vacancy = new Vacancy();
        vacancy.setLevelSalary(new LevelSalary());
        vacancy.setTargetDate(new TargetDate());
        return vacancy;
    }

    @Override
    protected SelectItem createSelectItem(Vacancy vacancy) {
        return new SelectItem(vacancy, vacancy.getTitle());
    }

    @Override
    protected String getNameEntity() {
        return "vacancyBean";
    }

    @Override
    protected VacancyBean createEntityBean() {
        return new VacancyBean();
    }

    public boolean isClosed(){
        return getEntityBean().isClosed();
    }

    public void setClosed(boolean closed){
       getEntityBean().setClosed(closed);
    }

    public boolean isEditingContactPersons() {
        return getEntityBean().isEditingContactPersons();
    }

    public void setEditingContactPersons(boolean editingContactPersons) {
        getEntityBean().setEditingContactPersons(editingContactPersons);
    }

    @Override
    public String preUpdate(){
        Vacancy vacancy = getService().getByIdWithContactPersons(getEntity().getId());
        setEntity(vacancy);
        changeState();
        return super.preUpdate();
    }

    public void changeState(){
        if(((Vacancy)getEntity()).getState().equals(State.CLOSED)) getEntityBean().setClosed(true);
        else getEntityBean().setClosed(false);
    }

    @Override
    public String view(){
       loadWithContactPersons();
       return super.view();
    }

    public void loadWithContactPersons(){
       Vacancy vacancy = getService().getByIdWithContactPersons(getEntity().getId());
       setEntity(vacancy);
    }

    public String cancel(){
        boolean creating = isNewEntity();
        setEditField(false);
        setNewEntity(false);
        if(creating) return list();
        else return form();
    }

    public void changeTargetDateForParam(ActionEvent event){
        TargetDate targetDate = ((Vacancy)getEntity()).getTargetDate();
        int count = targetDate.getCount();
        Time time = targetDate.getTime();
        final Calendar calendar = new GregorianCalendar();

        if(time == null) time = Time.MINUTE;

        switch (time) {
            case MINUTE:
                calendar.add(GregorianCalendar.MINUTE, count);
                break;
            case HOUR:
                calendar.add(GregorianCalendar.HOUR, count);
                break;
            case DAY:
                calendar.add(GregorianCalendar.DAY_OF_MONTH, count);
                break;
            case MONTH:
                calendar.add(GregorianCalendar.MONTH, count);
                break;
            case YEAR:
                calendar.add(GregorianCalendar.YEAR, count);
                break;
        }
        targetDate.setCloseUp(calendar.getTime());
    }

    //Enum 
    public List<SelectItem> getAppearanceSelectItems(){
        return EnumSelectItems.getSelectItems(Appearance.class);
    }

    public List<SelectItem> getTimeSelectItems(){
        return EnumSelectItems.getSelectItems(Time.class);
    }

    public List<SelectItem> getStateSelectItems(){
        return EnumSelectItems.getSelectItems(State.class);
    }

    public List<SelectItem> getCategorySelectionSelectItems(){
        return EnumSelectItems.getSelectItems(CategorySelection.class);
    }

    public List<SelectItem> getChannelSelectionSelectItems(){
        return EnumSelectItems.getSelectItems(ChannelSelection.class);
    }

    public List<SelectItem> getComplexitySelectItems(){
        List<SelectItem> selectItems = new ArrayList();
        for (int i=1; i<=4; i++ ){
            SelectItem selectItem = new SelectItem();
            selectItem.setLabel("" + i);
            selectItem.setValue(new Integer(i));
            selectItems.add(selectItem);
        }

        return selectItems;
    }

    public List<SelectItem> getPrioritySelectItems(){
        List<SelectItem> selectItems = new ArrayList();
        for (int i=1; i<=3; i++ ){
            SelectItem selectItem = new SelectItem();
            selectItem.setLabel("" + i);
            selectItem.setValue(new Integer(i));
            selectItems.add(selectItem);
        }

        return selectItems;
    }

    public void preEditingContactPersons(){
        getEntityBean().setEditingContactPersons(true);
    }

    public void afterEditingContactPersons(){
        getEntityBean().setEditingContactPersons(false);
    }

}
